package com.example.administrator.x2picturetransliteration.Util;

import android.app.Activity;
import android.os.Environment;
import android.util.Log;

import com.example.administrator.x2picturetransliteration.Util.Download.DownloadUtils;
import com.example.administrator.x2picturetransliteration.Util.Download.JsDownloadListener;
import com.orhanobut.logger.Logger;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import rx.Subscriber;

/**
 * 分享
 * Created by Administrator on 2019/3/29.
 */

public class UmengShareUtil {

    private Activity activity;

    //友盟分享回调
    private UMShareListener mUMShareListener;

    private String url; //分享链接
    private String Title; //标题
    private Integer Thumb; //缩略图
    private String Description; //描述

    public UmengShareUtil(final Activity activity){

        this.activity = activity;

        mUMShareListener = new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {

            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {

                new ToastUtil(activity).getToast(false,"分享成功").show();
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {

                Logger.e("分享异常" + throwable.getMessage());
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {

                new ToastUtil(activity).getToast(false,"分享取消").show();
            }
        };

    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public Integer getThumb() {
        return Thumb;
    }

    public void setThumb(Integer thumb) {
        Thumb = thumb;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public void Share(SHARE_MEDIA mSHARE_MEDIA){

        if (url==null || Title==null || Thumb==null || Description==null){

            new ToastUtil(activity).getToast(false,"参数不齐").show();
            return;
        }

        if ((mSHARE_MEDIA.equals(SHARE_MEDIA.WEIXIN) || mSHARE_MEDIA.equals(SHARE_MEDIA.QQ)) && url.indexOf("a.app.qq.com")==-1){

            Download(mSHARE_MEDIA);
            return;
        }

        UMWeb web = new UMWeb(url);
        web.setTitle(Title);
        web.setThumb(new UMImage(activity,Thumb));
        web.setDescription(Description);

        new ShareAction(activity)
                .setPlatform(mSHARE_MEDIA)//传入平台
                .withMedia(web)
                .setCallback(mUMShareListener)
                .share();

    }

    /**
     * 下载
     * */
    public void Download(final SHARE_MEDIA mSHARE_MEDIA){

        List<String> urls = Arrays.asList(url.split("//"));
        if (urls==null || urls.size()!=2)return;

        List<String> path = Arrays.asList(urls.get(1).split("/"));
        if (path==null || path.size()<2)return;

        String head = urls.get(0) + "//" + path.get(0) + "/";

        final String fileName = path.get(path.size()-1);

        String Route = "";

        for (int j=1;j<path.size()-1;j++){

            Route = Route + path.get(j) + "/";
        }

        final String SD = getSDPath();
        if (SD==null)return;


        LoadingDialogUtil.getInstance(activity).show();
        new DownloadUtils(head, new JsDownloadListener() {
            @Override
            public void onStartDownload() {

            }

            @Override
            public void onProgress(final int progress) {


//                if (progress>=100){
//
//                    LoadingDialogUtil.getInstance(activity).dismiss();
//                    ShareFileUtils.shareFile(activity,SD + "/" + fileName,mSHARE_MEDIA.equals(SHARE_MEDIA.WEIXIN)?"com.tencent.mm":"com.tencent.mobileqq");
//                }

            }

            @Override
            public void onFinishDownload() {

            }

            @Override
            public void onFail(String errorInfo) {

                String a = errorInfo;
            }

        }).download(Route + fileName, SD + "/" + fileName, new Subscriber() {
            @Override
            public void onCompleted() {
                LoadingDialogUtil.getInstance(activity).dismiss();
                ShareFileUtils.shareFile(activity,SD + "/" + fileName,mSHARE_MEDIA.equals(SHARE_MEDIA.WEIXIN)?"com.tencent.mm":"com.tencent.mobileqq");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Object o) {

            }
        });
    }

    public String getSDPath() {
        File sdDir = null;
        //判断sd卡是否存在
        boolean sdCardExist = Environment.getExternalStorageState()
                .equals(android.os.Environment.MEDIA_MOUNTED);
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();//获取根目录
            Log.e("qq", "外部存储可用..." + sdDir.toString());
        }
        return sdDir!=null?sdDir.toString():null;
    }

}
